﻿using Microsoft.VisualStudio;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using System;
using EnvDTE80;
namespace ClassMedataBrowser.Domain
{
	public class DTEUtils
	{
		public static IVsTextView GetTextCurrentTextView(DTE2 applicationObject)
		{
			Microsoft.VisualStudio.OLE.Interop.IServiceProvider provider = 
				(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)applicationObject;

			IntPtr refrence;
			Guid guid1 = typeof(SVsTextManager).GUID;
			Guid guid2 = typeof(IVsTextManager).GUID;

			int result = provider.QueryService(ref guid1, ref guid2, out refrence);

			if (ErrorHandler.Succeeded(result))
			{
				IVsTextManager2 manager = (IVsTextManager2)Marshal.GetObjectForIUnknown(refrence);
				IVsTextView resultView = null;
				manager.GetActiveView2(1, null,
					(uint)_VIEWFRAMETYPE.vftCodeWindow, out resultView);

				return resultView;
			}

			return null;
		}
	}
}
